package services

import (
	"gobuild/config"
	"gobuild/models"
	"io"
	"os"

	yaml "gopkg.in/yaml.v2"
)

//Job操作相关
type jobService struct{}

//保存Job
func (this *jobService) SaveJob(content map[string]interface{}) (err error) {

	JobName := content["JobName"].(string)
	if ok, err := config.PathExists(config.HomePath + "/jobs/" + JobName + "/config"); err != nil || !ok {
		os.MkdirAll(config.HomePath+"/jobs/"+JobName+"/config", 0777)
	}

	f, err := os.OpenFile(config.HomePath+"/jobs/"+JobName+"/config/config.yml", os.O_RDWR|os.O_CREATE, 0644)
	d, err := yaml.Marshal(content)

	_, err = io.WriteString(f, string(d)) //写入文件(字符串)
	defer f.Close()

	return
}

func (this *jobService) GetJobDropDown() (dropDownConfig models.JobDropDownConfig, err error) {

	dropDownConfig = models.JobDropDownConfig{}

	content, err := ConfigurationService.GetGlobalToolConfiguration()

	dropDownConfig.GTool = &content
	return
}
